package com.sskj.gocoin.p.fragment;

import com.lzy.okgo.OkGo;
import com.lzy.okrx2.adapter.FlowableResponse;
import com.sskj.gocoin.common.MarketWebSocket;
import com.sskj.gocoin.common.rxbus2.NetSubScriber;
import com.sskj.gocoin.m.HttpConfig;
import com.sskj.gocoin.m.bean.bean.BaseResponse;
import com.sskj.gocoin.m.bean.bean.NewStock;
import com.sskj.gocoin.m.http.JsonConverter;
import com.sskj.gocoin.p.base.BasePresenter;
import com.sskj.gocoin.utils.RxSchedulersHelper;
import com.sskj.gocoin.v.fragment.MarketListFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Lee on 2018/2/28 0028.
 */
public class MarketFragmentPst extends BasePresenter<MarketListFragment> {
    private MarketWebSocket btcWebSocket;
    String text = "{\"code\":\"all\"}";

//    /**
//     * 获取股票最新数据
//     *
//     * @param type 类型(1-股票;2-商品;3-指数)
//     * @param code 编码
//     */
//    public Flowable<com.lzy.okgo.model.Response<HttpResponse<List<NewStock>>>> getNewInfo(Integer type, String code, boolean update) {
//
//        return OkGo.<HttpResponse<List<NewStock>>>get(HttpConfig.BASE_URL + HttpConfig.MARKET_LIST)
//                .params("type", type)
//                .params("code", code)
//                .converter(new JsonConverter<HttpResponse<List<NewStock>>>() {
//                })
//                .adapt(new FlowableResponse<>());
//    }
//
//
//    public Flowable<com.lzy.okgo.model.Response<HttpResponse<Crowd>>> getCrowd() {
//        return OkGo.<HttpResponse<Crowd>>get(HttpConfig.BASE_URL + HttpConfig.CROWD)
//                .converter(new JsonConverter<HttpResponse<Crowd>>() {
//                })
//                .adapt(new FlowableResponse<>());
//    }

    public void getData(boolean update) {
        if (!update) {
            mView.loading();
        }
        OkGo.<BaseResponse<List<NewStock>>>get(HttpConfig.BASE_URL + HttpConfig.MARKET_LIST)
                .converter(new JsonConverter<BaseResponse<List<NewStock>>>() {
                })
                .adapt(new FlowableResponse<>())
                .compose(RxSchedulersHelper.transformer())
                .map(response -> {
                    if (response.body().getCode() == 200) {
                        return response.body().getData();
                    } else {
                        return new ArrayList<NewStock>();
                    }
                }).subscribe(new NetSubScriber<List<NewStock>>() {
            @Override
            public void onSuccess(List<NewStock> datas) {
                mView.setData(datas);
            }

            @Override
            public void onError(Throwable t) {
                super.onError(t);
                mView.stopLoading();
            }
        });

    }


    public void initWebSocket(int type) {
//        if (btcWebSocket == null) {
//            btcWebSocket = new MarketWebSocket(HttpConfig.BTC_WS, "marketList", text);
//            btcWebSocket.setListener(message -> mView.update(message));
//        }
    }

    public void closeWebSocket() {
        if (btcWebSocket != null) {
            btcWebSocket.closeWebSocket();
            btcWebSocket = null;
        }
    }

    @Override
    public void detachView() {
        super.detachView();
        closeWebSocket();
    }
}
